package com.example.sjly.ui.order;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sjly.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

//viewpager每个分页都装着这个
public class OrderListFragment extends Fragment {

    private Context mContext;
    private RecyclerView mOrderListRV;//列表view

    public static OrderListFragment newInstance(String label) {
        Bundle args = new Bundle();
        args.putString("label", label);
        OrderListFragment fragment = new OrderListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {//fragment绑定到activity
        super.onAttach(context);
        mContext=context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {//创建视图的方法
        View view = inflater.inflate(R.layout.view_order_list, container, false);//绑定布局文件
        mOrderListRV=view.findViewById(R.id.order_list_rv);//实例化RecyclerView
        List<Order> orderList=new ArrayList<>();
        for (int i=0;i<10;i++){//模拟数据
            orderList.add(new Order());
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        OrderRoomDatabase orderRoomDatabase=OrderRoomDatabase.getInstance(mContext);
        List<Order> orders=orderRoomDatabase.mDAO().getAll();
  //      Toast.makeText(mContext,orders.size()+"",Toast.LENGTH_SHORT).show();

        LinearLayoutManager layoutManager=new LinearLayoutManager(mContext);//布局管理器，线性从上到下
        OrderListAdapter adapter=new OrderListAdapter(mContext,orders);//RecyclerView的适配器
        mOrderListRV.setLayoutManager(layoutManager);//RecyclerView设置布局管理器
        mOrderListRV.setAdapter(adapter);//RecyclerView设置适配器
    }
}
